//search模块的小仓库

import { reqGetSearchInfo } from '@/api'

export default {
    namespaced: true,
    state: {
        searchList: {}
    },
    mutations: {
        GETSEARCHLIST(state, searchList) {
            state.searchList = searchList
        }
    },
    actions: {
        //获取search模块的数据
        async getSearchList({ commit }, params = {}) {
            //至少传递一个空对象
            //params形参：是当用户派发action的时候，第二个参数传递过来的，至少是一个空对象
            let result = await reqGetSearchInfo(params)
            if (result.code == 200) {
                commit('GETSEARCHLIST', result.data)
            }
        }
    },
    //计算属性：在项目中为简化数据而生
    getters: {
        attrsList(state) {
            //state,searchList,goodsList如果服务器数据回来了，没问题是一个数组
            //假如网络不给力|没有网state,searchList,goodsList应该返回的是undefined
            return state.searchList.attrsList || []
        },
        goodsList(state) {
            return state.searchList.goodsList || []
        },
        trademarkList(state) {
            return state.searchList.trademarkList || []
        },
        total(state) {
            return state.searchList.total
        }
    },
}